<?php


namespace App\Kuafu\Shop\Controller\Applet;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Customer\Service\CustomerPlatformService;
use App\Kuafu\Shop\Service\SearchService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class SearchController extends BaseController
{

    /**
     * 热门搜索
     * @param Request $request
     * @return JsonResponse
     */
    public function hotSearch(Request $request): JsonResponse
    {
        return $this->success(SearchService::hotSearch());
    }


    /**
     * 添加搜索log
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function addSearch(Request $request): JsonResponse
    {
        $this->validate($request, [
            'content' => 'required|string|min:0',
        ]);
        $customerId = CustomerPlatformService::getUserId($request);
        $shopId     = (int)$request->header('shopId', 0);
        $content    = (string)$request->get('content', '');

        return $this->success(SearchService::addSearch($customerId, $shopId, $content));
    }
}
